package com.likai.job;

import com.likai.entry.City;
import com.likai.entry.CityList;
import com.likai.service.CityDataService;
import com.likai.service.WeatherDataService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * 同步天气的job
 * Created by ex_likai3 on 2020/1/10.
 */
public class WeatherDataSyncJob extends QuartzJobBean {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(WeatherDataSyncJob.class);
    @Autowired
    private WeatherDataService weatherDataService;
    @Autowired
    private CityDataService cityDataService;
    
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LOGGER.info("Weather Data Sync Job. start");
        //获取城市列表
        List<City> cityList= null;
        try {
            cityList = cityDataService.listCity();
        } catch (Exception e) {
            LOGGER.error("Error",e);
        }
        //保存天气信息到redis中
        for (City city : cityList) {
            String cityId = city.getCityId();
            LOGGER.info("Weather Data Sync Job ,cityId = " + cityId);
            weatherDataService.syncDataByCityId(cityId);
        }

        LOGGER.info("Weather Data Sync Job. End");
    }
}
